/**
 @file dal.h

 @author  Copyright (C) 2012 Centec Networks Inc.  All rights reserved.

 @date 2012-4-9

 @version v2.0

*/
#ifndef _DAL_H_
#define _DAL_H_
#ifdef __cplusplus
extern "C" {
#endif

#include "dal_common.h"
#include "ctc_mix.h"

#define DAL_DEBUG_OUT(fmt, args...)            \
{ \
    if (g_dal_debug_on)  \
    {   \
        sal_printf(fmt, ##args);                        \
    }   \
}

#define DAL_DEBUG_DUMP(fmt, args...)            \
{ \
        sal_printf(fmt, ##args);                        \
}

#define DAL_MULTI_PP_DEV(dev_id) (DAL_ARCTIC_DEVICE_ID == dev_id)

typedef int32 (*dal_dma_finish_callback_t)(uint8 lchip);

struct dal_op_s
{
    int32   (* pci_read)(uint8 ldev, uint32 offset, uint32* value);
    int32   (* pci_write)(uint8 ldev, uint32 offset, uint32 value);
    int32   (* pci_conf_read)(uint8 ldev, uint32 offset, uint32* value);
    int32   (* pci_conf_write)(uint8 ldev, uint32 offset, uint32 value);
    int32   (* i2c_read)(uint8 ldev, uint16 offset, uint8 len, uint8* buf);
    int32   (* i2c_write)(uint8 ldev, uint16 offset, uint8 len, uint8* buf);
    int32   (* interrupt_register)(uint32 irq, int32 prio, void (*)(void*), void* data);
    int32   (* interrupt_unregister)(uint32 irq);
    int32   (* interrupt_set_en)(uint32 irq, uint32 enable);
    int32   (* interrupt_get_msi_info)(uint8 ldev, uint8* irq_base);
    int32   (* interrupt_set_msi_cap)(uint8 ldev, uint32 enable, uint32 num);
    int32   (* interrupt_set_msix_cap)(uint8 ldev, uint32 enable, uint32 num);
    uint64  (* logic_to_phy)(uint8 ldev, void* laddr);
    void* (* phy_to_logic)(uint8 ldev, uint64 paddr);
    uint32* (* dma_alloc)(uint8 ldev, int32 size, int32 dma_type);
    void    (* dma_free)(uint8 ldev, void* ptr);
    int32   (* dma_cache_inval)(uint8 ldev, uint64 ptr, int length);
    int32   (* dma_cache_flush)(uint8 ldev, uint64 ptr, int length);
    uintptr pcie_base_addr[CTC_MAX_LOCAL_CHIP_NUM];  /* Configure PCIe base address  in vxworks environment*/
    dal_pci_dev_t pci_dev;                    /* PCI_DEV Bus information, if not zero, means binding ldev to assigned physical pci  bus*/
    uint8  lchip;                             /* lchip base for this ldev, if not zero, means user assigned, otherwise lchip managed by SDK(default:0) */
    uint8  ldev;                              /* ldev id, used to identify which device to be init */
    int32   (* interrupt_get_irq)(uint8 ldev, uint8 type , uint16* irq_array, uint8* num);
    int32   (* handle_netif)(uint8 ldev, void* data);
    int32   (* pci_read_ext)(uint8 ldev, uint64 addr, uint32 len, uint32* p_value);
    int32   (* pci_write_ext)(uint8 ldev, uint64 addr, uint32 len, uint32* p_value);
    int32   (* pci_read_ext2)(uint8 ldev, uint64 addr, uint32 len, uint32* p_value, uint8 offset, uint16 real_len);
    int32   (* pci_write_ext2)(uint8 ldev, uint64 addr, uint32 len, uint32* p_value);
    int32   (* smi_read)(uint8 ldev, uint32 addr, uint32 len, uint32* p_value);
    int32   (* smi_write)(uint8 ldev, uint32 addr, uint32 len, uint32* p_value);
    int   (* dal_ioctl)(int fd, unsigned long int cmd, ...);
    uint8  pp_num;                            /* If unequal zero, means asssigned bu user, otherwise scan by SDK */
};
typedef struct dal_op_s dal_op_t;

extern dal_op_t g_dal_op;

struct dal_ldev_info_s
{
    uint8  lchip;             /*[out] lchip base for this ldev */
    uint8  pp_num;            /*[out] pipeline num */
    uint32 dev_id;            /*[out] DAL_XXX_DEVICE_ID*/
};
typedef struct dal_ldev_info_s dal_ldev_info_t;

int32 dal_op_init(dal_op_t* dal_op);
int32 dal_op_deinit(uint8 ldev);
int32 dal_set_device_access_type(dal_access_type_t device_type);
int32 dal_get_device_access_type(dal_access_type_t* p_device_type);
int32 dal_dma_debug_info(uint8 ldev);
int32 dal_get_dma_info(unsigned int ldev, void* p_info);
int32 dal_create_irq_mapping(uint32 hw_irq, uint32* sw_irq);
int32 dal_get_chip_dev_id(uint8 ldev, uint32 * dev_id);
int32 dal_dma_chan_register(uint8 ldev, void* data);
uint8 dal_device_get_pp_num(uint8 ldev);
int32 dal_get_ldev_info(uint8 ldev, dal_ldev_info_t* p_ldev);
int32 dal_knet_chan_register(uint8 ldev, void* data);
int32 dal_knet_set_wb_reloading(uint8 ldev, uint32 reloading_proc);
int32 dal_knet_dump(uint8 ldev, void* data);
void* dal_get_dev_addr(uint8 ldev);
bool dal_get_ldev_active(uint8 ldev);

#ifdef __cplusplus
}
#endif

#endif

